package cn.bxshey.modules_user.controller;

import cn.bxshey.feign.api.OrderClient;
import cn.bxshey.general.entity.User;

import cn.bxshey.general.Result.Result;
import cn.bxshey.general.aop.note.Permissions;
import cn.bxshey.general.aop.note.SystemLog;
import cn.bxshey.general.util.JwtUtils;
import cn.bxshey.modules_user.service.UserService;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户服务
 */

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Resource
    OrderClient orderClient;

    @Resource
    UserService userService;

    /**
     * 用户服务测试
     * @return String
     */
//    @Permissions(url = "/user/selectAll")
    @GetMapping("/test")
    public Result<String> testDemo() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            System.out.println(request.getHeader("feign-token"));
            System.out.println(request.getHeader("Authorization"));
        }
        System.err.println(orderClient.testDemo().getData());
        return Result.success("用户服务 测试成功！");
    }


    @SystemLog(value = "用户服务測試", serve = "1002")
    @GetMapping("/test/log")
    public Result<String> testDemo1(@RequestParam("id") Integer id, @RequestBody String str) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            System.out.println(request);
        }
        return Result.success("用户服务 测试成功！id---->" + id + "  str----->" + str);
    }


    @SystemLog(value = "測試2", serve = "1")
    @GetMapping("/test/log2")
    public Result<String> testDemo3(@RequestParam("id") Integer id, @RequestBody User user) {

        return Result.success("好的");
    }


    /**
     * 令牌生成
     */
    @GetMapping("/token/test")
    public Result<String> token() {
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("uid", "1001");
        stringStringMap.put("username", "哗啦啦");
        stringStringMap.put("ip", "0.0.0.0.0.0.0.1");
        try {
            System.out.println(orderClient.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(JwtUtils.createToken(stringStringMap));
    }


    /**
     * 查询所有用户信息接口
     */
    @GetMapping("/authentication/selectAll")
    public Result<List<User>> getAll() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token = request.getHeader("token");
        System.err.println(token);
        return Result.success(userService.selectGetAll());
    }


    /**
     * 根据用户id查询用户信息
     */
    @Permissions(url = "啊大大")
    @GetMapping("/selectById/{id}")
    @SystemLog(value = "根据用户id查询用户信息", serve = "1002")
    public Result<User> getByID(@PathVariable("id") Long id) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String access_tokens = request.getHeader("access_tokens");
        System.err.println("access_tokens---->" + access_tokens);
        return Result.success(userService.selectById(id));
    }


}
